Vue.component('Update', {
	template: `
		<el-dialog :title="form.name" width="600px" class="icon-dialog" :visible.sync="show" @open="open" :before-close="closeForm" append-to-body>
			<el-form :size="size" ref="form1" :model="form" :label-width=" ismobile()?'90px':'16%'">
				<el-row >
					<el-col :span="24">
						<el-form-item label="用户姓名" prop="name">
							<el-input v-model="form.name" autoComplete="off" clearable  placeholder="请输入用户姓名" :rules="[
								{ required: true, message: '用户姓名不能为空',trigger:'blur'}
								]"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row >
					<el-col :span="24">
						<el-form-item label="用户名" prop="username">
							<el-input  v-model="form.username" autoComplete="off" clearable  placeholder="请输入用户名"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="24">
						<el-form-item label="所属角色" prop="type">
							<el-select style="width:100%" v-model="form.role_id" filterable clearable placeholder="请选择">
								<el-option v-for="(item,i) in role_ids" :key="i" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row >
					<el-col :span="24">
						<el-form-item label="电话" prop="type">
							<el-input  v-model="form.phone" autoComplete="off" clearable  placeholder="请输入电话"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="24">
						<el-form-item label="单位" prop="org_name">
							<el-select v-model="form.org_name" placeholder="请选择" style="width: 90px;">
								<el-option label="全部" value=""></el-option>
								<el-option v-for="step_item in config.org_arr" :key="step_item.name" :label="step_item.name"
									:value="step_item.name">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row >
					<el-col :span="24">
						<el-form-item label="备注" prop="note">
							<el-input  v-model="form.note" autoComplete="off" clearable  placeholder="请输入备注"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row >
					<el-col :span="24">
						<el-form-item label="状态" prop="status">
							<el-switch :active-value="1" :inactive-value="0" v-model="form.status"></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row v-if="0">
					<el-col :span="24">
						<el-form-item label="创建时间" prop="create_time">
							<el-date-picker value-format="yyyy-MM-dd HH:mm:ss" type="date" v-model="form.create_time" clearable placeholder="请输入创建时间"></el-date-picker>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row >
					<el-col :span="24">
						<el-form-item label="余额" prop="balance">
							<el-input-number v-model="form.balance" autoComplete="off" clearable  placeholder="请输入余额"></el-input-number>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button :size="size" :loading="loading" type="primary" @click="submit" >
					<span v-if="!loading">确 定</span>
					<span v-else>提 交 中...</span>
				</el-button>
				<el-button :size="size" @click="closeForm">取 消</el-button>
			</div>
		</el-dialog>
	`
	,
	components: {
	},
	props: {
		show: {
			type: Boolean,
			default: false
		},
		size: {
			type: String,
			default: 'small'
		},
		info: {
			type: Object,
		},
	},
	data() {
		return {
			form: {
				name: '',
				user: '',
				role_id: '',
				note: '',
				status: 1,
				create_time: '',
				balance: 0.00
			},
			role_ids: [
				// { id: 1, name: "管理员" }
			],
			loading: false,
			rules: {
			}
		}
	},
	watch: {
		show(val) {
			if (val) {
				axios.post('/admin/role/index.html', {}).then(res => {
					if (res.data.status == 200) {
						this.role_ids = res.data.data.data
					} else {
						this.$message.error(res.data.msg);
					}
				})
			}
		}
	},
	methods: {
		open() {
			this.form = this.info
			this.form.create_time = parseTime(this.form.create_time)
		},
		submit() {
			let that = this
			if (!this.form.name) {
				this.$alert("请输入姓名", { type: "error" })
				return
			}
			if (!this.form.username) {
				this.$alert("请输入登陆用户名", { type: "error" })
				return
			}

			this.role_ids.forEach(function (role) {
				if (role.id == that.form.role_id) {
					that.form.role_name = role.name
				}
			})

			this.$refs['form1'].validate(valid => {
				if (valid) {
					this.loading = true
					axios.post('/admin/adminUser/update', this.form).then(res => {
						if (res.data.status == 200) {
							this.$message({ message: res.data.msg, type: 'success' })
							this.$emit('refesh_list')
							this.closeForm()
						} else {
							this.loading = false
							this.$message.error(res.data.msg)
						}
					}).catch(() => {
						this.loading = false
					})
				}
			})
		},
		closeForm() {
			this.$emit('update:show', false)
			this.loading = false
			if (this.$refs['form'] !== undefined) {
				this.$refs['form'].resetFields()
			}
		},
	}
})
